package fun.sssdnsy.controller;

import fun.sssdnsy.util.ConfigUtil;
import fun.sssdnsy.util.I18nUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import org.json.JSONObject;

/**
 * 设置窗口控制器
 */
public class SettingsController {

    @FXML
    private ComboBox<String> cmbLanguage;
    @FXML
    private ComboBox<String> cmbTheme;
    @FXML
    private ComboBox<String> cmbEncryption;
    @FXML
    private Button btnRestoreDefaults;
    @FXML
    private Button btnShortcutConfig;
    @FXML
    private Label lblTitle;

    private JSONObject config;

    private Stage stage;


    @FXML
    public void initialize() {
        config = ConfigUtil.loadConfig();

        // 初始化ComboBox的值
        lblTitle.setText(I18nUtil.getString("app.title"));
        cmbLanguage.getSelectionModel().select(config.optString("language", "auto"));
        cmbTheme.getSelectionModel().select(config.optString("theme", "light"));
        cmbEncryption.getSelectionModel().select(config.optString("encryptionAlgorithm", "SM4"));
    }



    @FXML
    private void onLanguageChange() {
        config.put("language", cmbLanguage.getValue());
        ConfigUtil.save();
    }

    @FXML
    private void onThemeChange() {
        config.put("theme", cmbTheme.getValue());
        ConfigUtil.save();
    }

    @FXML
    private void onEncryptionChange() {
        config.put("encryptionAlgorithm", cmbEncryption.getValue());
        ConfigUtil.save();
    }

    @FXML
    private void onShortcutConfig() {
         // ShortcutConfigDialog.show((Stage) btnShortcutConfig.getScene().getWindow());
    }

    @FXML
    private void onRestoreDefaults() {
        ConfigUtil.setDefaults();
        ConfigUtil.save();
    }

    public void setStage(Stage stage) {

        this.stage = stage;

    }
}
